package com.behelpful.common.integration.captcha.service;

import com.behelpful.common.integration.captcha.model.CaptchaVo;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.stereotype.Service;

/**
 * 验证码服务
 *
 * @author Alay
 * @date 2021-09-22 12:36
 */
@Service
public class CaptchaService {

    /**
     * 生成算数验证码
     *
     * @param width  图片宽
     * @param height 图片高
     * @param len    验证码随机字符长度 默认值 5
     * @return
     */
    public CaptchaVo genArithmeticCaptcha(int width, int height, int len) {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(width, height, len);
        // 验证码类型,默认字母数字混合
        captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        // 生成Base64 图片
        String imgBase64Str = captcha.toBase64();
        // 图片算数结算结果
        String result = captcha.text();

        CaptchaVo captchaVo = CaptchaVo.builder().result(result).base64Str(imgBase64Str).build();
        return captchaVo;
    }

}
